export const Base = {
    data () {
        return {
            data: [],
            total:0,
            modal:false,
            loading:false,
            modalTitle:'新增',
            id:'',
            single:false,
            arrData:[],
            single: false,
            filters:{
                page:1,
                rows:5,
                name:'',
                cateId:''
            }
        }
    },
    computed:{
        getTitile(){
            return this.modalTitle + this.moduleName;
        }
    },
    methods: {
        getData(){  
            let me = this;
            this.loading = true;
            this.$http({
                method:'post',
                url:`http://localhost:3000/${this.module}/list`,
                data:this.filters
            }).then(function(response) {
               me.loading = false;
               me.data = response.data.rows;
               me.total = response.data.total;
            });
        },
        handleSelectAll (status) {
            this.arrData = [];
            if(status){
                this.$refs.selection.selectAll(status);
                this.$refs.selection.data.forEach(element => {
                    this.arrData.push(element._id);
                });
            }
        },
        rowSelect (selection) {
            selection.forEach(element => {
                this.arrData.push(element._id);
            });
        },
        onChange(page){
           this.filters.page = page;
           this.getData();
        },
        onPageSizeChange(pageSize){
            this.filters.rows = pageSize;
            this.getData();
        },
        showModal (id) {
            if(!this.cateIdExit()){
                return;
            }
            this.modal = true;
            if(id){
                this.modalTitle = "编辑";
                this.findById(id);
                this.id = id;
            }else{
                this.modalTitle = "新增";
            }
        },
        findById(id){
            let me = this;
            this.$http({
                method:'get',
                url:`http://localhost:3000/${this.module}/data/${id}`
            }).then(function(response) {
                me.formValidate = response.data;
            });
        },
        removeConfirm(id){
               if(!id && this.arrData.length==0){
                 this.$Message.error('没有选中任何记录!');
                 return;
                }
              this.$Modal.confirm({
                title: '确认删除',
                content: '<h3>确认要删除记录吗？</h3>',
                onOk: () => {
                    if(id){
                        this.remove(id);
                    }else{
                        this.removeAll();
                    }
                }
            });
           
        },
        remove(id){
             let me = this;
             this.loading = true;
            this.$http({
                method:'delete',
                url:`http://localhost:3000/${this.module}/data/${id}`,
                data:{id:id}
            }).then(function(response) {
                me.$Message.success('操作成功!');
                me.getData();
            });
        },
        removeAll(){
            let me = this;
            this.loading = true;
           this.$http({
               method:'post',
               url:`http://localhost:3000/${this.module}/data/removes`,
               data:{ids:this.arrData.toString()}
           }).then(function(response) {
               me.$Message.success('操作成功!');
               me.getData();
           });
       },
        update(){
            let me = this;
            this.loading = true;
            this.$http({
                method:'put',
                url:`http://localhost:3000/${this.module}/data/${this.id}`,
                data:this.formValidate
            }).then(function(response) {
                me.$Message.success('操作成功!');
                me.getData();
            });
        },
        add(){
            if(this.cateIdExit()){
                this.formValidate.cateId = this.filters.cateId;
            }
            if(this.module=="users"){
                this.formValidate.password = md5(this.formValidate.password);
            }
            let me = this;
            this.loading = true;
            this.$http({
                method:'post',
                url:`http://localhost:3000/${this.module}/data`,
                data:this.formValidate
            }).then(function(response) {
                me.$Message.success('操作成功!');
                me.cancel();
                me.getData();
            });
        },
        ok(){
             this.$refs['formValidate'].validate((valid) => {
                if (valid) {
                    if(this.id){
                        this.update();
                    }else{
                        this.add();
                    }
                }
            });
        },
        cancel () {
            this.$refs['formValidate'].resetFields();
        },
        setCateId(){
            this.filters.page = 1;
            if(this.$refs.tree){
                let node = this.$refs.tree.getSelectedNodes()[0];
                this.filters.cateId = node && node._id;
                this.getData();
            }
        },
        cateIdExit(){
                if(this.$refs.tree && !this.filters.cateId){
                    this.$Message.info('请选择新闻类别!');
                    return false;
                }else{
                    return true;
                }
        },
        initFormatter(){  
            Date.prototype.Format = function(fmt) { //  
              let o = {    
                  "M+" : this.getMonth()+1,                 //月份    
                  "d+" : this.getDate(),                    //日    
                  "h+" : this.getHours(),                   //小时    
                  "m+" : this.getMinutes(),                 //分    
                  "s+" : this.getSeconds(),                 //秒    
                  "q+" : Math.floor((this.getMonth()+3)/3), //季度    
                  "S"  : this.getMilliseconds()             //毫秒    
              };    
              if(/(y+)/.test(fmt))    
                  fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));    
              for(var k in o)    
                  if(new RegExp("("+ k +")").test(fmt))    
                      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));    
              return fmt;    
            }  
          }  
    },
    created(){
        this.getData();    
        this.initFormatter();
    }
}